
package com.tedu.service.Impl;

import com.tedu.Common.enumerator.ServiceCode;
import com.tedu.Common.ex.ServiceException;
import com.tedu.dao.repository.OrdersRepository;
import com.tedu.pojo.entity.Orders;
import com.tedu.pojo.param.OrderAddNewParam;
import com.tedu.pojo.vo.HotelRoomVo;
import com.tedu.pojo.vo.HotelVo;
import com.tedu.pojo.vo.OrdersListVo;
import com.tedu.pojo.vo.TravelVo;
import com.tedu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Repository
@Slf4j
public class OrdersServiceImpl implements OrderService {

    @Autowired
    private OrdersRepository ordersRepository;


    public OrdersListVo listByUserId(Long userId) {
        OrdersListVo orderListVo = ordersRepository.listByUserId(userId);
        return orderListVo;
    }

    public Long AddOrders(OrderAddNewParam addNewParam) {
        log.debug("开始处理新增订单功能，参数：" + addNewParam);
        //判断订单类型为酒店还是旅游
        Integer type = addNewParam.getHotelId() == 0L ? 2 : 1;
        //生成对应的订单号
        String orderNo = createOrderNo(type);
        //查询订单号是否有重复
        OrdersListVo orderListVo = ordersRepository.GetByOrderNo(orderNo);
        if (orderListVo != null) {
            String msg = "订单号重复,程序进行修复";
            log.debug(msg + ":" + orderNo);
            orderNo = orderNo + "1";
        }
        /*
        * 判断所选择的酒店或者旅游项目是否存在
        * */
        log.debug("新增订单查询酒店/旅游项目是否存在，用户选择的酒店ID：" + addNewParam.getHotelId() + ",用户选择的旅游项目ID是：" + addNewParam.getTravelId());
        TravelVo travelVo = ordersRepository.selectByTravelId(addNewParam.getTravelId());
        HotelVo hotelListVo = ordersRepository.selectByHotelId(addNewParam.getHotelId());
        String msg;
        if (travelVo == null && addNewParam.getHotelId() == 0L) {
            msg = "所选择的旅游不存在，请检查";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, msg);
        }
        if (hotelListVo == null && addNewParam.getTravelId() == 0L) {
            msg = "所选择的酒店不存在，请检查";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, msg);
        }
        /*
        * 查询所选的酒店的房间类型是否存在
        * */
        HotelRoomVo hotelRoomVo =ordersRepository.selectByCategoryId(addNewParam.getHotelId(), addNewParam.getCategoryId());
        if (hotelRoomVo == null) {
            msg = "该酒店无所选的房间类型";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, msg);
        }
        /*
        * TODO 判断酒店对应的房间是否充足
        * */
        log.debug("新增订单查询酒店库存是否充足，用户：" + addNewParam.getUserId() + "用户提交数量：" + addNewParam.getNum());


        Orders orders = new Orders();
        orders.setOrderNo(orderNo);
        orders.setOrderType(type);
        /*
        * 计算酒店/旅游的价格
        * */
        if (orders.getOrderType() == 1) {
            log.debug("酒店价格计算开始！");
            HotelRoomVo Hotelprice = ordersRepository.selectByCategoryId(addNewParam.getHotelId(), addNewParam.getCategoryId());
            Double price = Hotelprice.getPrice() * (double) addNewParam.getNum();
            orders.setPrice(price);
        } else if (orders.getOrderType() == 2) {
            log.debug("旅游价格计算开始！");
        }
        orders.setPaymentMethod(0);
        orders.setOrderPayState(1);
        orders.setOrderState(3);
        BeanUtils.copyProperties(addNewParam, orders);
        int insert = ordersRepository.insertAddOrderNew(orders);
        if (insert == 0) {
            msg = "服务器忙，请稍后在进行尝试";
            throw new ServiceException(ServiceCode.ERROR_INSERT, msg);
        }
        return orders.getId();

    }

    /*
     * 生成订单号方法
     * */
    private String createOrderNo(Integer OrderType) {
        /*
         * orderType订单类型：1-酒店，2-旅游，3-酒店/旅游，4-其他
         * */
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String format = now.format(formatter);
        if (OrderType == 1) {
            //酒店订单
            format = "H" + formatter;
        }
        if (OrderType == 2) {
            //旅游订单
            format = "T" + formatter;
        }
        //TODO 3-酒店/旅游，4-其他  后期。。。
        return format;
    }


}
